package com.justgame.project.generator.schema;

import com.google.common.base.CaseFormat;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
public class Enumerate {
    private final String fieldName;
    private final String enumName;
    private final List<Item> enumItems = new ArrayList<>();

    private Enumerate(String fieldName){
        this.fieldName = fieldName;
        this.enumName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,fieldName);
    }

    private Enumerate(String fieldName,String enumName){
        this.fieldName = fieldName;
        this.enumName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,enumName);
    }

    public static Enumerate builder(String fieldName){
        return new Enumerate(fieldName);
    }

    public static Enumerate builder(String fieldName,String enumName){
        return new Enumerate(fieldName,enumName);
    }

    public Enumerate item(@NonNull String name,@NonNull String description){
        name = name.replaceAll(" ","");
        Assert.isTrue(StringUtils.hasLength(name) && name.length() <= 50,"枚举值 "+fieldName+" 不符合预期标准：枚举值长度不符。");
        Assert.hasLength(description,"枚举值 "+fieldName+" 不符合预期标准：必须设置枚举值描述。");
        enumItems.add(new Item(name,description));
        return this;
    }

    @Getter
    public static class Item{
        private final String name;
        private final String description;

        public Item(String name, String description) {
            this.name = name;
            this.description = description;
        }
    }
}
